<script type="text/javascript">
function myKeypress(event){
    return (/[\d\.]/.test(String.fromCharCode(event.keyCode||event.charCode)));
}

function myFocus(){
	var v = parseFloat($.formatNumber(this.value, "##0.00"), 10);
	if(isNaN(v)){
		v = 0;
	}
	this.value = v;
	$(this).data("before", v);
}

function myPaste(){
	return false;
}

function myChange(){
	var v = parseFloat(this.value, 10);
	if(isNaN(v)){
		v = 0;
	}
	this.value = v;
}

function myBlur(){
	var v = parseFloat($.formatNumber(this.value, "##0.00"), 10);
        v = isNaN(v) ? $(this).data("before") : v;
	//set value
	this.value = $.formatNumber(v, "###,##0.00")

    //get row name
    var row_name = $(this).attr("name")

        row_name = row_name.substring(0, row_name.indexOf('.'))

        //debugger;
    //get quantity
    var quantity = $("input[name='"+row_name+".quantity']").val()
        quantity = parseFloat($.formatNumber(quantity, "##0.00"), 10);
    //get price
    var price = $("input[name='"+row_name+".price']").val()
        price = parseFloat($.formatNumber(price, "##0.00"), 10);
    //calc amount
    var amount = quantity.mul(price)
        amount = $.formatNumber(amount, "###,##0.00")
    //set span amount
    $("span[name='"+row_name+".amount']").html(amount)
    //set input amount
    $("input[id='"+row_name+".amount']").val(amount)   
}

$(document).ready(function() {
    //datepicker
	$('[datepicker="datepicker"]').each(function(){
		$(this).datepicker({
			  dateFormat: 'yy-mm-dd'
			, minDate: new Date(new Date().getTime() )
			, onClose: function(dateText, inst) {
				$('[name="'+$(this).attr('target') + '_year"]').val(inst.selectedYear)
				$('[name="'+$(this).attr('target') + '_month"]').val(inst.selectedMonth + 1)
				$('[name="'+$(this).attr('target') + '_day"]').val(inst.selectedDay)
			}
		}).attr("readonly", true)
    });
	$("#makerInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'pickplanAjax', action: 'staffInstanceList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#maker\\.id").val(ui.item.id);
        }
    });
   //director
	$("#directorInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'pickplanAjax', action: 'staffInstanceList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#director\\.id").val(ui.item.id);
        }
    });
    //detail dealer
	$('[name$="dealerInput"]').each(function(){
        var name = this.name
        $(this).autocomplete({
            minLength: 0,
            source: function(request, response) {
               $.ajax({
                    url: "${createLink(controller:'pickplanAjax', action: 'dealerInstanceList')}",
                    data: { term: request.term },
                    dataType: "json",
                    type: "POST",
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            return{
                                id : item[0],
                                value: item[1]+'-'+item[2],
                                label: item[1]+'-'+item[2]
                            }
                        }));
                    }
                });
            },
            select: function( event, ui ) {
                $(this).next().val(ui.item.id);
            }
        });
    });
    $('input[name$="quantity"]').each(function(){
		$(this).keypress(myKeypress).focus(myFocus).blur(myBlur).bind('paste', myPaste).change(myChange)
	});
    $('input[name$="price"]').each(function(){
		$(this).keypress(myKeypress).focus(myFocus).blur(myBlur).bind('paste', myPaste).change(myChange)
	});
/*
    $("input[name='serialNumber']").each(function(){
        $(this).keypress(function(){
            alert(event.keyCode||event.charCode)
        })
    })
*/
});
</script>
